package com.grouppurchase.controller;

import com.grouppurchase.common.Result;
import com.grouppurchase.entity.User;
import com.grouppurchase.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理", description = "提供用户相关的管理接口")
public class UserController {

    @Autowired
    private UserService userService;
    
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "根据微信openid进行用户登录或注册")
    public Result<User> login(@ApiParam(value = "用户openid", required = true) @RequestParam String openid,
                             @ApiParam(value = "用户unionid", required = false) @RequestParam String unionid,
                             @ApiParam(value = "用户昵称", required = false) @RequestParam String nickname,
                             @ApiParam(value = "用户头像URL", required = false) @RequestParam String avatar) {
        User user = userService.createOrUpdateUser(openid, unionid, nickname, avatar);
        return Result.success(user);
    }
    
    @GetMapping("/info/{userId}")
    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
    public Result<User> getUserInfo(@ApiParam(value = "用户ID", required = true) @PathVariable Long userId) {
        User user = userService.getById(userId);
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("用户不存在");
    }
    
    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息", notes = "根据用户ID更新用户基本信息")
    public Result<Boolean> updateUserInfo(@ApiParam(value = "用户ID", required = true) @RequestParam Long userId,
                                         @ApiParam(value = "用户昵称", required = false) @RequestParam(required = false) String nickname,
                                         @ApiParam(value = "用户头像URL", required = false) @RequestParam(required = false) String avatar,
                                         @ApiParam(value = "用户手机号", required = false) @RequestParam(required = false) String phone) {
        boolean success = userService.updateUserInfo(userId, nickname, avatar, phone);
        return success ? Result.success() : Result.error("更新失败");
    }

    @GetMapping("/invite/code/{userId}")
    @ApiOperation(value = "获取邀请码", notes = "根据用户ID获取用户的邀请码")
    public Result<Map<String, String>> getInviteCode(@ApiParam(value = "用户ID", required = true) @PathVariable Long userId) {
        User user = userService.getById(userId);
        if (user != null) {
            Map<String, String> result = new HashMap<>();
            result.put("inviteCode", user.getInviteCode());
            return Result.success(result);
        }
        return Result.error("用户不存在");
    }

    @PostMapping("/invite/handle")
    @ApiOperation(value = "处理邀请", notes = "处理用户邀请关系")
    public Result<Boolean> handleInvite(@ApiParam(value = "被邀请人ID", required = true) @RequestParam Long inviteeId,
                                       @ApiParam(value = "邀请码", required = false) @RequestParam(required = false) String inviteCode) {
        boolean success = userService.handleUserInvite(inviteeId, inviteCode);
        return success ? Result.success() : Result.error("邀请处理失败");
    }
}
